package cn.yeziji.forum.factory.websocket;

import cn.yeziji.forum.common.websocket.Chatroom;
import cn.yeziji.forum.factory.websocket.enums.WebsocketFactoryOption;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Websocket操作执行者
 *
 * @author gzkemays
 * @date 2022/4/25
 * @deprecated to netty
 */
public class WebsocketOperationExecutioner {
  public static String DEFAULT_METHOD = "defaultMethod";

  public static Object execute(Chatroom chatroom) {
    String key = chatroom.getKey();
    if (key.equals(Chatroom.PRIVATE) && chatroom.getMsg() == null) {
      // 聊天室为私密且发送信息为空，意味着创建私密聊天室，返回聊天记录。
      return WebsocketOperationFactory.operation(
          WebsocketFactoryOption.CHAT_ROOM_RECORDS, chatroom);
    } else if (key.equals(Chatroom.PRIVATE_CLOSE)) {
      // 提示私密聊天对象，聊天者已退出聊天室
      return WebsocketOperationFactory.operation(WebsocketFactoryOption.CHAT_ROOM_CLOSE, chatroom);
    } else if (key.equals(Chatroom.USER_MSG)) {
      // 获取用户信息
      return WebsocketOperationFactory.operation(WebsocketFactoryOption.GET_USER_MSG, chatroom);
    } else if (key.equals(Chatroom.USER_RECORDS)) {
      // 获取指定用户聊天记录
      return WebsocketOperationFactory.operation(WebsocketFactoryOption.GET_USER_RECORDS, chatroom);
    } else if (key.equals(Chatroom.ALL)) {
      return WebsocketOperationFactory.operation(WebsocketFactoryOption.CHAT_ROOM_ALL, chatroom);
    } else if (key.equals(Chatroom.ALL_CLOSE)) {
      return WebsocketOperationFactory.operation(
          WebsocketFactoryOption.CHAT_ROOM_ALL_CLOSE, chatroom);
    } else {
      // 发送信息
      return WebsocketOperationFactory.operation(WebsocketFactoryOption.CHAT_ROOM_SEND, chatroom);
    }
  }

  public static Object execute(Chatroom chatroom, Object additional) {
    String key = chatroom.getKey();
    if (key.equals(Chatroom.SYSTEM) && chatroom.getFrom().equals(Chatroom.SYSTEM_FROM)) {
      // 系统推送消息
      return WebsocketOperationFactory.operation(
          WebsocketFactoryOption.SYSTEM_PUSH_MSG, chatroom, additional);
    }
    return null;
  }

  public static Object execute(WebsocketFactoryOption option, Object data, Object additional) {
    Class<?> clazz = option.getClazz();
    try {
      if (data != null && additional == null) {
        Constructor<?> constructor = clazz.getConstructor(Object.class);
        Object obj = constructor.newInstance(data);
        Method declaredMethod = clazz.getDeclaredMethod(option.getMethod());
        declaredMethod.setAccessible(true);
        return declaredMethod.invoke(obj);
      }
      if (additional != null) {
        Constructor<?> constructor = clazz.getConstructor(Object.class, Object.class);
        Object obj = constructor.newInstance(data, additional);
        Method declaredMethod = clazz.getDeclaredMethod(option.getMethod());
        declaredMethod.setAccessible(true);
        return declaredMethod.invoke(obj);
      }
    } catch (NoSuchMethodException
        | InvocationTargetException
        | InstantiationException
        | IllegalAccessException e) {
      e.printStackTrace();
    }
    return null;
  }
}
